/**
 * 部门js
 */
var userInfoReport = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				userInfoReport.curPage = 1;
				userInfoReport.setPageChange(null);
				userInfoReport.search();
			}
		};
		
		jsUtils.controlFunctionBtn();
	},

	getChannelResource : function() {
		var url = 'mgr/userChannelCtrl/list';
		var params = {
			'curPage' : 0,
			'pageSize' : 9999
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var option = '<option value="">请选择</option>';
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					option = option + '<option value="' + singleObj.channelId + '">'
						+ singleObj.name + '</option>';
				}
				$('select[name="channel"]').append(option);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');

	},
	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			userInfoReport.curPage = 1;
			userInfoReport.setPageChange(null);
			userInfoReport.search();
		});

		 
		
		$('#excel').on('click', function() { 
			var url = '../../../mgr/userStatisticsController/excel';
			if (typeof (pageIdx) != "undefined") {
				userInfoReport.curPage = pageIdx;
			}

			 
			var userRealName = $.trim($('#searchModForm input[name="userRealName"]').val());
			var channel = $.trim($('#searchModForm select[name="channel"]').val());
			 
			
			location.href = url+"?userRealName=" + userRealName 
				+ "&channel=" + channel;
		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/userStatisticsController/total';
	
		if (typeof (pageIdx) != "undefined") {
			userInfoReport.curPage = pageIdx;
		}

	 
		var userRealName = $.trim($('input[name="userRealName"]').val());
		var channel = $.trim($('#searchModForm select[name="channel"]').val());
		 
		
		var params = {
			'userRealName' : userRealName,
			'channel' : channel,
			'curPage' : userInfoReport.curPage,
			'pageSize' : userInfoReport.pageSize
		};

        var cb = function (data) {
            if (data.resultCode == 200) {
                var totalCount = data.size;
                var pageTotal = (totalCount / userInfoReport.pageSize > 0)
                    ? Math.ceil(totalCount / userInfoReport.pageSize) : 1;


                pageHelper.init(pageTotal, totalCount, userInfoReport.search,
                    delCurPage); // 翻页事件
            }
        };
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = 'mgr/userStatisticsController/list';
		if (typeof (pageIdx) != "undefined") {
			userInfoReport.curPage = pageIdx;
		}

	 
		var userRealName = $.trim($('input[name="userRealName"]').val());
		var channel = $.trim($('#searchModForm select[name="channel"]').val());
		 
	 
		var params = {
				'userRealName' : userRealName,
				'channel' : channel,
				'curPage' : userInfoReport.curPage,
				'pageSize' : userInfoReport.pageSize
		};
		
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';
			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {
					var singleObj = data.result[i];
					
					tr += '<tr><td><input type="hidden" name="id" value="'
							+ singleObj.userId
							+ '"/>'
							+ (i + 1)
							+ '</td><td>'
							+ (singleObj.userRealName != null?singleObj.userRealName:"无")
                            + '</td><td>'
                            + singleObj.userPhoneNum
							+ '</td><td>'
							+ singleObj.relationUserAmount
							+ '</td><td>'
							+ singleObj.tieCardAmount
							+ '</td><td>'
							+ singleObj.bespeakOrderAmount
							+ '</td><td>'
							+ singleObj.payOrderAmount
							+ '</td><td>'
							+ singleObj.payOrderMoney
							+ '</td><td>'
							//+ singleObj.totalPoints
							//+ '</td><td>'
							//+ singleObj.usedPoints
							//+ '</td><td>'
							//+ singleObj.leftPoints
							//+ '</td><td>'
							+ singleObj.userDutName
							+ '</td><td>'
							+ singleObj.userDepName
						 	+ '</td></tr>';
				}
			} else {
				tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			userInfoReport.bindEventOtherBtn();
			jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		
	},

	init : function() {
		userInfoReport.getChannelResource();
		userInfoReport.setPageChange(null);
		userInfoReport.search();
		userInfoReport.startEvent();

		userInfoReport.bindEvent();
		jsUtils.controlFunctionBtn();
		
	}
};

$(document).ready(function() {
	userInfoReport.init();
});

//提供给外部刷新调用
function refashData() {
	userInfoReport.setPageChange(null);
	userInfoReport.search();
}
